package com.unittec.zk.provider.auth.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.service.HystrixRestTemplateService;
import com.unittec.zk.sdk.auth.domain.SysDicItemDto;
import com.unittec.zk.sdk.root.internal.exception.BusinessException;

/**
 * @Description:
 * @author: chenjiawang
 * @CreateDate: 2020/6/16 9:49
 */
@Service
public class BaseService {

    @Autowired
    private SysDicItemService sysDicItemService;
    @Autowired
    private HystrixRestTemplateService hystrixRestTemplateService;

    @Autowired
    private Log log;

    public List<SysDicItemDto> getDicInfoList(String dicCode) {

        List<SysDicItemDto> dicItemListByDicCode = sysDicItemService.findDicItemListByDicCode(dicCode);
        return dicItemListByDicCode == null ? new ArrayList<>() : dicItemListByDicCode;
    }

    /**
     * 获取数据字典映射信息
     *
     * @param type
     * @param paramMap
     * @return
     */
    public String getDicMapping(String dicCode, String type, Map<String, Object> paramMap) {
        String typeClassStr = getDicMapping(dicCode, type);
        paramMap.put("typeClass", typeClassStr);
        return typeClassStr;
    }

    public String getDicMapping(String dicCode, String type) {
        String typeClassStr = "";
        if (StringUtils.isBlank(type)) {
            throw new BusinessException("导出类型不能为空 ");
        } else {
            List<SysDicItemDto> dicInfoList = null;
            try {
                dicInfoList = getDicInfoList(dicCode);
            } catch (Exception e) {
                throw new BusinessException("查询数据字典出错", e);
            }
            if (dicInfoList != null && dicInfoList.size() > 0) {
                for (SysDicItemDto map : dicInfoList) {
                    if (type.equalsIgnoreCase(map.getItemText())) {
                        typeClassStr = map.getItemValue() == null ? "" : map.getItemValue();
                    }
                }
            } else {
                throw new BusinessException("查询不到相应的导出类型");
            }
        }
        return typeClassStr;
    }

    @Cacheable(cacheNames = "getDicText")
    public String getDicMappingText(String dicCode, String type) {
        String typeClassStr = "";
        if (StringUtils.isBlank(type)) {
            throw new BusinessException("导出类型不能为空 ");
        } else {
            List<SysDicItemDto> dicInfoList = null;
            try {
                dicInfoList = getDicInfoList(dicCode);
            } catch (Exception e) {
                throw new BusinessException("查询数据字典出错", e);
            }
            List<String> list = Arrays.asList(type.split(","));
            List<String> list1 = new ArrayList<>();
            if (dicInfoList != null && dicInfoList.size() > 0) {
                for (String str : list) {
                    for (SysDicItemDto map : dicInfoList) {
                        if (str.equalsIgnoreCase(map.getItemValue())) {
                            typeClassStr = map.getItemText() == null ? "" : map.getItemText();
                            list1.add(typeClassStr);
                        }
                    }
                }
                typeClassStr = String.join(",", list1);
            } else {
                throw new BusinessException("查询不到相应的导出类型");
            }
        }
        return typeClassStr;
    }

    /**
     *
     * @param dicCode
     * @return
     */
    public String[] getDicItems(String dicCode) {
        if (StringUtils.isBlank(dicCode)) {
            throw new BusinessException("字典编码不能为空 ");
        } else {
            List<SysDicItemDto> dicInfoList = null;
            try {
                dicInfoList = getDicInfoList(dicCode);
            } catch (Exception e) {
                throw new BusinessException("查询数据字典出错", e);
            }
            if (dicInfoList != null && dicInfoList.size() > 0) {
                return dicInfoList.stream().map(SysDicItemDto::getItemText).toArray(String[]::new);
            } else {
                return new String[0];
            }
        }
    }
}
